<?php
/**
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

/**
 * 分拣配货API 196订单模块
 * User: rui
 * Email: poptser@foxmail.com
 * Date: 2016/10/10
 * Time: 14:59
 */

namespace SDXApi\Controller;

use Think\Model;
use Think\Log;

class SpecialdistributionController extends AppController
{

    public $wid;


    public function _initialize($apptoken = '', $apptime = '', $check_uid = false, $session_id = '', $check_sign = true)
    {
        parent::_initialize($apptoken, $apptime, $check_uid, $session_id, false);
        $this->wid = I("post.wid",15);
    }


    /**
     * 获取所有配送员配货员列表
     */
    public function get_sorting(){
        $data['ph'] = M("warehouse_personnel")->where(array("position"=>1,"status"=>1,"phone"=>array("neq","")))->select();
        $data['ps'] = M("warehouse_personnel")->where(array("position"=>2,"status"=>1,"phone"=>array("neq","")))->select();
        $this->returnApiSuccess($data,'success');
    }
    /**
     * 未配货列表
     * rui poptser@foxmail.com 2017-10-14 09:52:29
     * @param bool $internal  内部数据
     * @return int
     */
    public function undeploy_list($internal = false){
        $user_id=I('post.user_id','intval');
        if(!isset($user_id)){
            returnApiError('参数错误');
        }
        $order_info = M("order_info");

        $where['shipping_id'] = 3;                  //配送单
        $where['sorting_id']=0;
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=0;	//备货中
        $where['wid']=$this->wid;

        $page  = I("post.page");
        if($page<0||empty($page)){
            $page = 1;
        }
        $strat_num = ($page-1)*100;
        $count = $order_info->field('order_id,order_sn,wid')
            ->where($where)->count();
        if($internal){
            return $count;
        }
        $list_array = $order_info->field('order_id,order_sn,wid')
            ->where($where)->order('order_id desc')->limit($strat_num,100)->select();
        if(count($list_array)==100){
            $is_bottom  = 0;    //还有
        }else{
            $is_bottom  = 1;    //尾页
        }
        $this->returnApiSuccess(
            array(
                'list'=>$list_array, //订单列表
                'is_bottom' =>$is_bottom,
                'count'=>$count  //未配货
            ),'success');
    }

    /**
     * 配货中列表
     * rui poptser@foxmail.com 2017-10-14 16:10:29
     * @param bool $internal  内部数据
     * @return int
     */
    public function deploy_list($internal = false){
        $user_id=I('post.user_id','intval');
        if(!isset($user_id)){
            returnApiError('参数错误');
        }
        $order_info = M("order_info");

        $where['shipping_id'] = 3;                  //配送单
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=3;	//备货中
        $where['wid']=$this->wid;

        $page  = I("post.page");
        if($page<0||empty($page)){
            $page = 1;
        }
        $strat_num = ($page-1)*100;
        $count = $order_info->field('order_id,order_sn,wid')
            ->where($where)->count();
        if($internal){
            return $count;
        }
        $list_array = $order_info->field('order_id,order_sn,wid')
            ->where($where)->order('order_id desc')->limit($strat_num,100)->select();

        if(count($list_array)==100){
            $is_bottom  = 0;    //还有
        }else{
            $is_bottom  = 1;    //尾页
        }

        $this->returnApiSuccess(
            array(
                'list'=>$list_array, //订单列表
                'count'=>$count,  //未配货
                'is_bottom' =>$is_bottom
            ),'success');
    }

    /**
     * 配货完成未质检列表
     * rui poptser@foxmail.com 2017-10-14 16:10:29
     * @param bool $internal  内部数据
     * @return int
     */
    public function unquality_list($internal = false){
        $user_id=I('post.user_id','intval');
        if(!isset($user_id)){
            returnApiError('参数错误');
        }
        $order_info = M("order_info");
        $where['sorting_id'] = array("neq",0);      //已有配送员
        $where['shipping_id'] = 3;                  //配送单
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=5;	//备货中
        $where['quality_id']=0;	//未质检
        $where['wid']=$this->wid;

        $page  = I("post.page");
        if($page<0||empty($page)){
            $page = 1;
        }
        $strat_num = ($page-1)*100;
        $count = $order_info->field('order_id,order_sn,wid')
            ->where($where)->count();
        if($internal){
            return $count;
        }
        $list_array = $order_info->field('order_id,order_sn,wid')
            ->where($where)->order('order_id desc')->limit($strat_num,100)->select();
        if(count($list_array)==100){
            $is_bottom  = 0;    //还有
        }else{
            $is_bottom  = 1;    //尾页
        }
        $this->returnApiSuccess(
            array(
                'list'=>$list_array, //订单列表
                'is_bottom' =>$is_bottom,
                'count'=>$count  //未配货
            ),'success');
    }
    /**
     * 已质检/未打单列表
     * rui poptser@foxmail.com 2017-10-14 16:10:29
     * @param bool $internal  内部数据
     * @return int
     */
    public function unbag_list($internal = false){
        $user_id=I('post.user_id','intval');
        if(!isset($user_id)){
            returnApiError('参数错误');
        }
//        $order_info = M("order_info");
//        $where['sorting_id'] = array("neq",0);      //已有配送员
//        $where['shipping_id'] = 3;                  //配送单
//        $where['pay_status']=array('in','2,3');
//        $where['order_status']= array('neq',2);
//        $where['shipping_status']=5;	        //配货完成
//        $where['quality_id']=array('neq',0);;	//已质检
//        $where['wid']=$this->wid;

        $page  = I("post.page");
        if($page<0||empty($page)){
            $page = 1;
        }
        $strat_num = ($page-1)*100;

        $where = "oi.shipping_id = 3 AND oi.sorting_id != 0 AND oi.outgo = 0 AND oi.pay_status in(2,3)
        AND oi.order_status !=2 AND oi.shipping_status = 5 AND oi.quality_id !=0
         AND (oie.bag_num =0 OR oie.order_id IS NULL )
         AND wid = ".$this->wid;

        $sql="SELECT count(oi.order_id) AS order_num FROM sdx_order_info AS oi
LEFT JOIN sdx_order_info_ext AS oie ON oi.order_id = oie.order_id
WHERE ".$where;
        $count = M()->query($sql);
        if($internal){
            return $count[0]['order_num'];
        }
        $sql="SELECT oi.order_id,oi.order_sn,oi.wid FROM sdx_order_info AS oi
LEFT JOIN sdx_order_info_ext AS oie ON oi.order_id = oie.order_id
WHERE ".$where." LIMIT ".$strat_num." , 100";
        $list_array = M()->query($sql);
        //dump($list_array);
        if(count($list_array)==100){
            $is_bottom  = 0;    //还有
        }else{
            $is_bottom  = 1;    //尾页
        }
        $this->returnApiSuccess(
            array(
                'list'=>$list_array, //订单列表
                'is_bottom' =>$is_bottom,
                'count'=>$count[0]['order_num']  //统计数
            ),'success');
    }
    /**
     * 已打单/未出仓列表
     * rui poptser@foxmail.com 2017-10-14 16:10:29
     * @param bool $internal  内部数据
     * @return int
     */
    public function unoutgo_list($internal = false){
        $user_id=I('post.user_id','intval');
        if(!isset($user_id)){
            returnApiError('参数错误');
        }
        //$order_info = M("order_info");

//        $where['shipping_id'] = 3;                  //配送单
//        $where['sorting_id'] = array("neq",0);      //已有配送员
//        $where['outgo'] = 0;                        //未出仓
//        $where['pay_status']=array('in','2,3');
//        $where['order_status']= array('neq',2);
//        $where['shipping_status']=5;	            //已配货
//        $where['quality_id']=array('neq',0);;	    //已质检
//        $where['wid']=$this->wid;


        $page  = I("post.page");
        if($page<0||empty($page)){
            $page = 1;
        }
        $strat_num = ($page-1)*100;

        $where = "oi.shipping_id = 3 AND oi.sorting_id != 0 AND oi.outgo = 0 AND oi.pay_status in(2,3)
        AND oi.order_status !=2 AND oi.shipping_status = 5 AND oi.quality_id !=0
         AND oie.bag_num > 0
         AND wid = ".$this->wid;

        $sql="SELECT count(oi.order_id) AS order_num FROM sdx_order_info AS oi
LEFT JOIN sdx_order_info_ext AS oie ON oi.order_id = oie.order_id
WHERE ".$where;
        $count = M()->query($sql);
        if($internal){
            return $count[0]['order_num'];
        }
        $sql="SELECT oi.order_id,oi.order_sn,oi.wid FROM sdx_order_info AS oi
LEFT JOIN sdx_order_info_ext AS oie ON oi.order_id = oie.order_id
WHERE ".$where." LIMIT ".$strat_num." , 100";
        $list_array = M()->query($sql);
        if(count($list_array)==100){
            $is_bottom  = 0;    //还有
        }else{
            $is_bottom  = 1;    //尾页
        }
        $this->returnApiSuccess(
            array(
                'list'=>$list_array, //订单列表
                'is_bottom' =>$is_bottom,
                'count'=>$count[0]['order_num']  //统计数
            ),'success');
    }

    /**
     * 已出仓/未配送列表
     * rui poptser@foxmail.com 2017-10-14 16:10:29
     * @param bool $internal  内部数据
     * @return int
     */
    public function outgo_list($internal = false){
        $user_id=I('post.user_id','intval');
        if(!isset($user_id)){
            returnApiError('参数错误');
        }
        $order_info = M("order_info");

        $where['shipping_id'] = 3;                  //配送单
        $where['sorting_id'] = array("neq",0);      //已有配送员
        $where['outgo'] = 1;                        //未出仓
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=5;	            //已配货
        $where['quality_id']=array('neq',0);;	    //已质检
        $where['wid']=$this->wid;

        $page  = I("post.page");
        if($page<0||empty($page)){
            $page = 1;
        }
        $strat_num = ($page-1)*100;
        $count = $order_info->field('order_id,order_sn,wid')
            ->where($where)->count();
        $list_array = $order_info->field('order_id,order_sn,wid')
            ->where($where)->order('order_id desc')->limit($strat_num,100)->select();

        if($internal){
            return $count;
        }
        if(count($list_array)==100){
            $is_bottom  = 0;    //还有
        }else{
            $is_bottom  = 1;    //尾页
        }
        $this->returnApiSuccess(
            array(
                'list'=>$list_array, //订单列表
                'is_bottom' =>$is_bottom,
                'count'=>$count  //统计数
            ),'success');
    }

    /**
     * 配送中列表
     * rui poptser@foxmail.com 2017-10-14 16:10:29
     * @param bool $internal  内部数据
     * @return int
     */
    public function distribute_list($internal = false){
        $user_id=I('post.user_id','intval');
        if(!isset($user_id)){
            returnApiError('参数错误');
        }
        $order_info = M("order_info");

        $where['shipping_id'] = 3;                  //配送单
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=1;
        $where['quality_id']=array('neq',0);	    //已质检
        $where['wp_userid']=array('neq',0);
        $where['wid']=$this->wid;

        $page  = I("post.page");
        if($page<0||empty($page)){
            $page = 1;
        }
        $strat_num = ($page-1)*100;
        $count = $order_info->field('order_id,order_sn,wid')
            ->where($where)->count();

        $list_array = $order_info->field('order_id,order_sn,wid')
            ->where($where)->order('order_id desc')->limit($strat_num,100)->select();

        if($internal){
            return $count;
        }
        if(count($list_array)==100){
            $is_bottom  = 0;    //还有
        }else{
            $is_bottom  = 1;    //尾页
        }
        $this->returnApiSuccess(
            array(
                'list'=>$list_array, //订单列表
                'is_bottom' =>$is_bottom,
                'count'=>$count  //统计数
            ),'success');
    }
    /**
     * 配送完成列表
     * rui poptser@foxmail.com 2017-10-14 16:10:29
     * @param bool $internal  内部数据
     * @return int
     */
    public function distributed_list($internal = false){
        $user_id=I('post.user_id','intval');
        if(!isset($user_id)){
            returnApiError('参数错误');
        }
        $order_info = M("order_info");

        $where['shipping_id'] = 3;                  //配送单
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=2;
        $where['wid']=$this->wid;

        $page  = I("post.page");
        if($page<0||empty($page)){
            $page = 1;
        }
        $strat_num = ($page-1)*100;
        $count = $order_info->field('order_id,order_sn,wid')
            ->where($where)->count();

        $list_array = $order_info->field('order_id,order_sn,wid')
            ->where($where)->order('order_id desc')->limit($strat_num,100)->select();

        if($internal){
            return $count;
        }
        if(count($list_array)==100){
            $is_bottom  = 0;    //还有
        }else{
            $is_bottom  = 1;    //尾页
        }
        $this->returnApiSuccess(
            array(
                'list'=>$list_array, //订单列表
                'is_bottom' =>$is_bottom,
                'count'=>$count  //统计数
            ),'success');
    }


    /**
     *订单状态操作
     */
    public function action_order(){
        $action = I("post.action");
        $user_id = I("post.user_id");
        $order_array = explode(",",I("post.order_id"));
        $i= 0;
        switch ($action){
            case "undeploy_list":   //开始配货
                foreach ($order_array as $key=>$value){
                    $result = $this->deploy_order_action($user_id,$value);
                    if($result===false){
                        $i++;
                    }
                }
                echo $i;
                break;
            case "deploy_list":     //配货完成
                foreach ($order_array as $key=>$value){
                    $result = $this->deployed($value);
                    if($result===false){
                        $i++;
                    }
                }
                echo $i;
                break;
            case "unquality_list":  //质检通过
                foreach ($order_array as $key=>$value){
                    $result = $this->quality_goods($user_id,$value);
                    if($result===false){
                        $i++;
                    }
                }
                echo $i;
                break;
            case "unbag_list":  //打印分袋单
                foreach ($order_array as $key=>$value){
                    $result = $this->order_bag($user_id,$value);
                    if($result===false){
                        $i++;
                    }
                }
                echo $i;
                break;
            case "unoutgo_list":  //出仓订单
                foreach ($order_array as $key=>$value){
                    $result = $this->outgo_action($user_id,$value);
                    if($result===false){
                        $i++;
                    }
                }
                echo $i;
                break;
            case "outgo_list":  //开始配送
                foreach ($order_array as $key=>$value){
                    $result = $this->distribute($user_id,$value);
                    if($result===false){
                        $i++;
                    }
                }
                echo $i;
                break;
            case "distribute_list":  //完成配送
                foreach ($order_array as $key=>$value){
                    $result = $this->distributed($user_id,$value);
                    if($result===false){
                        $i++;
                    }
                }
                echo $i;
                break;
        }

    }
    /**
     * 开始配货
     * rui poptser@foxmail.com 2016-10-11 10:09:19
     * @param $user_id
     * @param $order_id
     * @return bool
     */
    private function deploy_order_action($user_id, $order_id){
        $where['shipping_id'] = 3;      //配送单
        $where['order_id'] = $order_id;
        $where['sorting_id']=0;
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=0;    //备货中
        $where['wid']=$this->wid;
        $data['sorting_id'] = $user_id;
        $data['shipping_status'] = 3;   //修改为配货中
        $result = M("order_info")->where($where)->save($data);    //开始配货
        if($result){
            //修改成功，写入日志
            $order_info = M("order_info")->where("order_id = ".$order_id)->find();
            $a=array(
                'order_id'=>$order_id,
                'user_id'=>$user_id,
                'order_status'=>$order_info['order_status'],
                'shipping_status'=>3,
                'pay_status'=>$order_info['pay_status'],
                'to_user'=>1,
                'action_note'=>'开始配货'
            );
            $this->record_diliveryman_log($a);//记录配送员操作日志
            $this->record_logistics($order_id,3,"开始配货");
            return true;
        }else{
            return false;
        }
    }
    /**
     * 完成配货
     * rui poptser@foxmail.com 2016-10-11 14:28:17
     * @param $order_id
     * @return bool
     */
    private function deployed($order_id){
        $where['shipping_id'] = 3;      //配送单
        $where['order_id'] = $order_id;
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=3;    //配货中
        $where['wid']=$this->wid;
        $data['shipping_status'] = 5;   //修改为完成配货
        $data['sorting_time'] = gmtime();
        $result = M("order_info")->where($where)->save($data);
        if($result){
            //修改成功，写入日志
            $order_info = M("order_info")->field("sorting_id,order_status,pay_status")->where("order_id = ".$order_id)->find();
            $a=array(
                'order_id'=>$order_id,
                'user_id'=>$order_info['sorting_id'],
                'order_status'=>$order_info['order_status'],
                'shipping_status'=>5,
                'pay_status'=>$order_info['pay_status'],
                'action_note'=>'配货完成'
            );
            //dump($a);
            $this->record_diliveryman_log($a);//记录配送员操作日志
            $this->record_logistics($order_id,5,"配货完成");
            return true;
        }else{
            return false;
        }
    }

    /**
     * 商品质检确认
     * @param $user_id
     * @param $order_id
     * @return bool
     */
    private function quality_goods($user_id, $order_id){
        $where['shipping_id'] = 3;      //配送单
        $where['order_id'] = $order_id;
        $where['pay_status']=array('in','2,3');
        $where['order_status']= array('neq',2);
        $where['shipping_status']=5;    //备货中
        $where['wid']=$this->wid;

        $data['quality_id'] = $user_id;
        $data['quality_time'] = gmtime();

        $result = M("order_info")->where($where)->save($data);
        if($result){
            //修改成功，写入日志
            $order_info = M("order_info")->where("order_id = ".$order_id)->find();
            $a=array(
                'order_id'=>$order_id,
                'user_id'=>$user_id,
                'order_status'=>$order_info['order_status'],
                'shipping_status'=>5,
                'pay_status'=>$order_info['pay_status'],
                'to_user'=>1,
                'action_note'=>'商品质量已确认'
            );
            $this->record_diliveryman_log($a);//记录配送员操作日志
            $this->record_logistics($order_id,5,"商品质量已确认");
            return true;
        }else{
            return false;
        }
    }
    /**
     * 订单分袋数
     * @param $user_id
     * @param $order_id
     * @return bool
     */
    private function order_bag($user_id, $order_id){
        $normal_num = I("post.normal_num");   //常温袋数
        $cold_num = I("post.cold_num");       //冷藏袋数
        $ice_num = I("post.ice_num");         //冷冻袋数

        $add['order_id'] = $order_id;
        $add['bag_num'] = $bag_num =  $normal_num+$cold_num+$ice_num;
        $str = "常温:".$normal_num." 冷藏:".$cold_num." 冷冻:".$ice_num;

        $order_model = M("order_info_ext")->add($add,$options=array(),$replace=true);
        if($order_model){
            $order_info = M("order_info")->field("order_status,shipping_status,pay_status")
                ->where("order_id = ".$order_id)->find();
            $a=array(
                'order_id'=>$order_id,
                'user_id'=>$user_id,
                'order_status'=>$order_info['order_status'],
                'shipping_status'=>$order_info['shipping_status'],
                'pay_status'=>$order_info['pay_status'],
                'action_note'=>'打印质检单，总数：'.$bag_num.", ".$str
            );
            $this->record_diliveryman_log($a);
            $this->record_logistics($order_id,$order_info['shipping_status'],'打印质检单，总数：'.$bag_num.", ".$str);
            return true;
        }else{
            return false;
        }
    }
    /**
     * 出仓订单
     * @param $user_id
     * @param $order_id
     * @return bool
     */
    private function outgo_action($user_id,$order_id){
        $driver_name = $_POST['driver_name'];   //司机姓名
        $car_sn = $_POST['car_sn'];             //车牌号
        $go_time = $_POST['go_time'];           //出发司机
        $to_time = $_POST['to_time'];           //预期到达司机

        $where['order_status'] = array("neq",2);    //订单未取消
        $where['pay_status'] = array("in","2,3");   //支付状态为已支付/部分退款
        $where['wid'] = $this->wid;   //仓库ID
        $where['shipping_id'] = 3;    //配送单
        $where['order_id'] = $order_id;    //

        $where['shipping_status'] = 5;
        $where['outgo'] = 0;
        $where['quality_id'] = array("neq",0);     //已质检
        $data['outgo'] =  1;
        $data['outgo_id'] =  $user_id;
        $data['outgo_time'] =  gmtime();
        $action = "订单已出仓<br/>司机:".$driver_name." 车牌号:".$car_sn." 出发时间:".$go_time." 预期到达时间:".$to_time;

        //修改已到站
        $data['station'] = 1;
        $data['station_id'] = $user_id;
        $data['station_time'] = gmtime();
        $actions ="<br/>自动到站";

        $Result = M("order_info")->where($where)->save($data);

        if($Result){
            $order_info = M("order_info")->field("order_status,shipping_status,pay_status")
                ->where("order_id = ".$order_id)->find();
            $a=array(
                'order_id'=>$order_id,
                'user_id'=>$user_id,
                'order_status'=>$order_info['order_status'],
                'shipping_status'=>$order_info['shipping_status'],
                'pay_status'=>$order_info['pay_status'],
                'to_user'=>1,
                'action_note'=>$action.$actions
            );
            $this->record_diliveryman_log($a);//记录配送员操作日志
            $this->record_logistics($order_id,$order_info['shipping_status'],"已出仓");
            return true;
        }else{
            return false;
        }
    }
    /**
     * 开始配送
     * @param $user_id
     * @param $order_id
     * @return bool
     */
    private function distribute($user_id,$order_id){
        $where['order_id']=$order_id;
        $where['station']=1;    //只能配送已到站的订单
        $where['pay_status']=array("in","2,3");
        $where['order_status']=array("neq","2");
        $where['wid']=$this->wid;
        $where['shipping_status']=5;
        $where['shipping_id'] = 3;    //配送单

        $list=M('order_info')->field('order_status,shipping_status,pay_status')->where($where)->find();
        //dump(M('order_info')->getLastSql());
        if($list){
            $data['order_status']=5;
            $data['shipping_status']=1;
            $data['shipping_time']=gmtime();
            $data['wp_userid'] = $user_id;
            $data['station'] = 2; //开始配送 修改到站状态为已出站

            $id=M('order_info')->where($where)->save($data);
            if($id){
                $a=array(
                    'order_id'=>$order_id,
                    'user_id'=>$user_id,
                    'order_status'=>5,
                    'shipping_status'=>1,
                    'pay_status'=>$list['pay_status'],
                    'action_note'=>'开始配送',
                    'to_user'=>1
                );
                $this->record_diliveryman_log($a);//记录配送员操作日志
                $remark['phone'] = M('warehouse_personnel')->where(array('uid'=>$user_id))->getField('phone');
                $name = M('warehouse_personnel')->where(array('uid'=>$user_id))->getField('name');
                $this->record_logistics($order_id,1,"订单已由:".$name." [ ".$remark['phone']." ] 开始配送");
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    /**
     * 完成配送
     * @param $user_id
     * @param $order_id
     * @return bool
     */
    private function distributed($user_id,$order_id){
        $where['order_id']=$order_id;
        $where['pay_status']=array("in","2,3");
        $where['order_status']=array("neq","2");
        $where['shipping_status']=1;

        $list=M('order_info')->
            field('user_id,wid,order_sn,order_id,order_status,shipping_status,pay_status')
            ->where($where)->find();
        if($list){
            $data['shipping_status']=2;
            $data['confirm_time']=gmtime();
            $data['wp_userid']=$user_id;
            $id=M('order_info')->where($where)->save($data);
            if($id){
                $a=array(
                    'order_id'=>$list['order_id'],
                    'user_id'=>$user_id,
                    'order_status'=>5,
                    'shipping_status'=>2,
                    'pay_status'=>$list['pay_status'],
                    'to_user'=>1,
                    'action_note'=>'配送完成,收货确认'
                );
                $this->record_diliveryman_log($a);//记录配送员操作日志
                $this->record_logistics($list['order_id'],2,"确认收货");
                $this->order_buckle_inventory($list);
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 返回数据
     * @param array $data
     * @param null $msg
     */
    private  function returnApiSuccess($data=array(), $msg=null){
        header("Content-type: text/html; charset=utf-8");
        $data['status']=1;
        $data['error_msg']=$msg;
        echo json_encode($data);
        exit;
    }

    /**
     * 写入订单物流信息
     * @param $order_id
     * @param $shipping_status
     * @param $remark
     */
    private function record_logistics($order_id, $shipping_status, $remark){
        $data['log_time']=gmtime();
        $data['remark']=$remark;
        $data['shipping_status']=$shipping_status;
        $data['order_id']=$order_id;
        M('logistics')->add($data);
    }

    /**
     * 记录配送员操作日志
     * @param array $a
     * @param array $extend_data
     * @return mixed
     */
    private function record_diliveryman_log($a=array(), $extend_data=array()){
        $data['order_id']=$a['order_id'];
        $user=$this->get_admin_user_name($a['user_id']);
        if(!$user){
            return false;
        }
        $wp=M('warehouse_personnel')->where(array('uid'=>$a['user_id'],'status'=>1))->find();;

        $data['con_phone'] = $wp['phone'];

        $juese='';
        if($wp){
            $data['longitude'] = $wp['longitude'];
            $data['latitude'] = $wp['latitude'];
            if($wp['position']==1){
                $juese='配货';
            }elseif ($wp['position']==2){
                $juese='配送';
            }elseif ($wp['position']==3){
                $juese='仓管';
            }
        }
        if(!empty($extend_data)){
            $data['action_user']=$extend_data['shipping_exman_name']."[.".$user['user_account']."]—角色：".$juese."   ".$extend_data['shipping_exman_mobile'];
        }else{
            if($wp['phone']){
                $data['action_user']=$user['user_name']." [ ".$wp['phone']." ] ".$juese;
            }else{
                $data['action_user']=$user['user_name']." ".$juese;
            }
        }

        $client_type = I("post.client_type");
        $version = I("post.version");
        if($client_type==1){
            $client_name = "Android";
        }elseif ($client_type==2){
            $client_name = "iOS";
        }else{
            $client_name = "未知";
        }

        $data['order_status']=$a['order_status'];
        $data['shipping_status']=$a['shipping_status'];
        $data['pay_status']=$a['pay_status'];
        $data['action_place']=0;
        $data['action_note']=$a['action_note']." By ".$client_name." [ ".$version." ]";
        $data['action_type'] = empty($a['action_type']) ? 0 : $a['action_type'] + 0;    //预警类型

        $data['log_time']=gmtime();
        $data['user_id']=$a['user_id'];
        $data['to_user']=$a['to_user'];


        $res = M('order_action')->add($data);
        return $res;
    }


    /**
     * 根据后台用户ID获取当前用户数据
     * @param $uid
     * @return bool|mixed
     */
    private function get_admin_user_name($uid){
        $res=M('admin_user')->where(array('user_id'=>$uid))->find();
        if($res){
            $warehouse_user = M('warehouse_personnel')->field('name,phone')->where(array('uid'=>$uid))->find();
            $res['phone'] = $warehouse_user['phone'];
            $res['name'] = $warehouse_user['name'];
            $res['user_account'] = $res['user_name'];
            if(!empty($res['name'])){
                $res['user_name'] = $res['name']." [ ".$res['user_name']." ]";
            }
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 收货确认减冻结库存
     * @param $order
     * @return bool
     */
    private function order_buckle_inventory($order){
        $is_shipping_status = M("order_action")->where(array("order_id"=>$order['order_id'],"shipping_status"=>array("in",array(2,6))))->count();
        if($is_shipping_status){
            return false;
        }
        $wid=$order['wid'];
        $where='(order_id='.$order['order_id'].' and is_presell=0) or (order_id='.$order['order_id'].' and is_presell=1 and buckle_inventory=1)';
        $order_goods=M('order_goods')->where($where)->select();
        if($order_goods){
            $goodsstorage_mod=D('Goodsstorage');
            $goodsfreezerecord=D('Goodsfreezerecord');
            $refund_detail=M('refund_detail');
            $rdgoods=array();
            //检查退货退款时是否已经减掉了冻结库存，若有，该操作则不进行减操作
            $rdlist=$refund_detail->field('goods_id,SUM(refund_number) as refund_number')
                ->where(array('order_id'=>$order['order_id']))
                ->group('goods_id')
                ->select();
            if($rdlist){
                foreach ($rdlist as $key=>$val){
                    $rdgoods[$val['goods_id']]=$val['refund_number'];
                }
            }
            foreach ($order_goods as $k=>$v){
                $gsdata=$goodsstorage_mod->get_warehous_goods($wid,$v['goods_id']);
                if($gsdata){
                    if(!empty($rdgoods[$v['goods_id']])){
                        $refund_number=$rdgoods[$v['goods_id']];
                        $v['goods_number']=$v['goods_number']-ceil($refund_number);
                    }
                    if($v['goods_number']==0){// 过滤订单商品购买数据为0时，不插入冻结库存记录
                        continue;
                    }
                    //减去冻结库存
                    $goodsstorage_mod->save_freeze_theactual_goods($wid,$v['goods_id'],$v['goods_number']);
                    $goodsfreezerecorddata=array(
                        'goods_storage_id'=>$gsdata['id'],
                        'wid'=>$wid,
                        'goods_id'=>$v['goods_id'],
                        'goods_sn'=>$v['goods_sn'],
                        'goods_name'=>$v['goods_name'],
                        'freeze_num'=>$v['goods_number'],
                        'real_num'=>0,
                        'frozen_num'=>$gsdata['freeze_num']-$v['goods_number'],
                        'defective_num'=>0,
                        'operation_userid'=>0,
                        'average_userid'=>$order['user_id'],
                        'operation_addtime'=>gmtime(),
                        'source_no'=>$order['order_sn'],
                        'record_type'=>6,
                        'remark'=>'确认收货',
                    );
                    //生成冻结库存记录
                    $goodsfreezerecord->add_freeze_record($goodsfreezerecorddata);
                }else{
                    continue;
                }
            }
        }else{
            return false;
        }
    }


    /**
     * 加载概览信息
     */
    public function get_overflow(){
        $data = array(
            "undeploy_list" => $this->undeploy_list(true),  //未配货列表
            "deploy_list" => $this->deploy_list(true),      //配货中列表
            "unquality_list" => $this->unquality_list(true),//未质检列表
            "unbag_list" => $this->unbag_list(true),        //未打单列表
            "unoutgo_list" => $this->unoutgo_list(true),    //未出仓列表
            "outgo_list" => $this->outgo_list(true),        //未配送列表
            "distribute_list" => $this->distribute_list(true),//配送中列表
            "distributed_list" => $this->distributed_list(true)//配送完列表
        );

        $this->returnApiSuccess(
            array(
                'list'=>$data
            ),'success');
    }

    /**
     * 获取仓库列表
     */
    public function get_wid_data(){
        $warehouse['list'] = M("warehouse")->where("pid > 0")->field("id,name")->select();
        $this->returnApiSuccess($warehouse,'success');
    }
}